/*******************************************************************************
 * NES Mapper for BBK
 *
 *  Author:  <fanoble@yeah.net>
 *
 *  Create:   2014-06-24, by fanoble
 *******************************************************************************
 */

#include "WaveOut.h"

class	MapperBBK : public Mapper
{
public:
	MapperBBK( NES* parent ) : Mapper(parent) { Init(); }

	void	Init();
	void	Reset();

	~MapperBBK();

//------------------------------------------------------------------------------

	static int FeedLPC( void* host, unsigned char* food );

	static DWORD WINAPI LPCThreadProc( LPVOID lpParam );

//------------------------------------------------------------------------------

	// 8000 - FFFF
	BOOL	ReadHigh( WORD addr, LPBYTE pdata );
	void	Write( WORD addr, BYTE data );

	// 4100 - 7FFF
	BYTE	ReadLow ( WORD addr );
	void	WriteLow( WORD addr, BYTE data );

#define INNO_CS_NONE		0
#define INNO_CS_ROM			1
#define INNO_CS_DRAM		2
#define INNO_CS_HOLTEK		3
	DWORD	MapAddr( WORD addr, int* cs_type, BOOL is_write );

//------------------------------------------------------------------------------
	void	PPU_Latch( WORD addr );
	LPBYTE  GetExtPMEM( int addr );
	LPBYTE  GetExtVMEM( int addr );
	LPBYTE	GetPatternTable( int scanline, int addr );

	BYTE	ExCmdRead ( EXCMDRD cmd );

	BOOL	CheckIRQ();
//------------------------------------------------------------------------------

	void	Clock( INT nCycles );
	void	HSync( INT scanline );

//------------------------------------------------------------------------------

protected:
private:

	// inno asic
	BOOL	bMapRam;  // FF01 D3
	BOOL	bFF01_D4;
	BYTE	bRomSel_89AB;
	BYTE	nRegFF14; // 7 bit
	BYTE	nRegFF1C; // 6 bit
	BYTE	nRegFF24; // 6 bit
	BYTE	nRegFF2C; // 6 bit
	BYTE	nRegSPInt; // 1bit

//------------------------------------------------------------------------------

	// holtek asic
	INT		bSplitMode;

	BOOL	bEnableIRQ;
	INT		nLineCount;

	INT		nNrOfSR;
	INT		nNrOfVR;
	BYTE	QueueSR[32];
	BYTE	QueueVR[32];

	INT		nQIndex;
//------------------------------------------------------------------------------
	INT		nCurScanLine;

	BOOL	bDiskAccess;
//------------------------------------------------------------------------------

	// for ROM and RAM
	LPBYTE	BIOS;
	LPBYTE	DISK;
	BYTE	EVRAM[32 * 1024];
	BYTE	EDRAM[512 * 1024]; // 8K / Page

//------------------------------------------------------------------------------

#define SP_BUF_SIZE 16
	BYTE	nSPData[SP_BUF_SIZE];
	int		nSPReadPos;
	int		nSpWritePos;

	int		nResetLPC;

	BOOL    bRunLPC;
	HANDLE  reqLPC;
	HANDLE  ackLPC;
	HANDLE  thdLPC;
};

/*******************************************************************************
                           E N D  O F  F I L E
*******************************************************************************/
